package net.lbtech.util;

import org.apache.commons.lang3.StringUtils;

public class CommonUtil {
	public static int objectToInteger(Object obj) {
		try {
			if (obj != null && !obj.toString().trim().equals(""))
				return Integer.parseInt(obj.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
			return 0;
		}
		return 0;
	}

	/**
	 *城市字符串切割
    */
	public static byte[] IntToByteArray(String value)
	{

		byte[] src = new byte[4];
		src[0] = (byte) Integer.parseInt(StringUtils.substring(value,0,2));
		src[1] =  (byte) Integer.parseInt(StringUtils.substring(value,2,4));
		src[2] = (byte) Integer.parseInt(StringUtils.substring(value,4,6));
		src[3] = 0;
		return src;
	}
	//将高字节在前转为int，低字节在后的byte数组(与IntToByteArray2想对应)
	public static int ByteArrayToInt(byte[] bArr) {
		if(bArr.length!=4){
			return -1;
		}
		return (int) ((((bArr[0] & 0xff) << 24)
				| ((bArr[1] & 0xff) << 16)
				| ((bArr[2] & 0xff) << 8)
				| ((bArr[3] & 0xff) << 0)));
	}

	//byte转16进制字符串
	public static String toHex(byte b) {
		String result = Integer.toHexString(b & 0xFF);
		if (result.length() == 1) {
			result = '0' + result;
		}
		return result;
	}


}
